﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Salon
{
    public partial class RegisterOrders : Form
    {
        public Order ChosenOrder { get; set; }
        public int Mode; // Tryb pracy formularza
        
        public RegisterOrders()
        {
            Mode = 0;
            InitializeComponent();

            dgvOrderGrid.AutoGenerateColumns = false;
            Text = Forms.ORDER_REGISTER;

            btnChoose.Enabled = btnChoose.Visible = false;
        }

        public RegisterOrders(bool dict)
        {
            Mode = 0;
            InitializeComponent();
            dgvOrderGrid.AutoGenerateColumns = false;
            Text = Forms.ORDER_REGISTER;
            if (dict)
            {
                btnAdd.Enabled = btnChoose.Visible = false;
                btnEdit.Enabled = btnChoose.Visible = false;
                btnDelete.Enabled = btnChoose.Visible = false;
                btnChoose.Enabled = btnChoose.Visible = true;
                btnChoose.Top = btnAdd.Top;
                Mode = 1; // Wybieranie
            }
            else 
            {
                btnAdd.Enabled = true;
                btnEdit.Enabled = true;
                btnDelete.Enabled = true;
                btnChoose.Enabled = btnChoose.Visible = false;
            }
        }

        private void RegisterOrders_Load(object sender, EventArgs e)
        {
            cmbSalon.Initialize();

            FillTable();
        }

        private void FillTable()
        {
            BindingSource bs = new BindingSource();
            OrderService us = new OrderService();
            IList<IFilter> fl = Global.getFilterList(this); // filtry z formatki

            IList<Order> Orders = ServiceFactory.getOrderService().getOrders(fl);

            foreach (Order Order in Orders)
                bs.Add(Order);

            dgvOrderGrid.DataSource = bs;
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            FillTable();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            foreach (IFilter filtr in Global.getFilterList(this))
            {
                filtr.CleanUp();
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Global.MainPage.openNewOrderForm(null);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {

            if (dgvOrderGrid.SelectedRows.Count != 1)
            {
                MessageBox.Show("Proszę zaznaczyć dokładnie jeden wiersz do edycji.");
                return;
            }

            Global.MainPage.openNewOrderForm((Order)dgvOrderGrid.SelectedRows[0].DataBoundItem);
        }

        private void dgvOrderGrid_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            // UPRAWNIENIA
            if ((Global.currentUser.Role & (int)(Roles.Administrator | Roles.SuperAdministrator)) == 0)
                return;

            if (Mode == 1)
            {
                // jeśli wybieranie - podwójne klikmnięcie wybiera a nie edytuje
                this.ChosenOrder = (Order)dgvOrderGrid.Rows[e.RowIndex].DataBoundItem;
                this.Close();
            }
            else
                if (!Global.MainPage.checkIfFormOpen(Forms.CAR_FORM))
                {
                    FormOrder fc = new FormOrder((Order)dgvOrderGrid.Rows[e.RowIndex].DataBoundItem);
                    fc.MdiParent = Global.MainPage;
                    fc.Text = Forms.ORDER_FORM;
                    fc.Show();
                }
        }

    }
}
